package com.hjm.project.merPaid.merPaid.controller;

import java.util.List;
import java.util.Map;

import com.hjm.common.exception.BusinessException;
import com.hjm.common.utils.StringUtils;
import com.hjm.common.utils.Utils;
import com.hjm.project.accounts.accounts.service.IAccountsService;
import com.hjm.project.bankInfo.bankInfo.domain.QbBankInfo;
import com.hjm.project.bankInfo.bankInfo.service.IQbBankInfoService;
import com.yxhd.encrypt.EncryptUtils;
import org.apache.commons.collections.CollectionUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.hjm.framework.aspectj.lang.annotation.Log;
import com.hjm.framework.aspectj.lang.enums.BusinessType;
import com.hjm.project.merPaid.merPaid.domain.MerPaid;
import com.hjm.project.merPaid.merPaid.service.IMerPaidService;
import com.hjm.framework.web.controller.BaseController;
import com.hjm.framework.web.domain.AjaxResult;
import com.hjm.common.utils.poi.ExcelUtil;
import com.hjm.framework.web.page.TableDataInfo;

import javax.annotation.Resource;

/**
 * 打款数据Controller
 * 
 * @author LOVE
 * @date 2020-11-27
 */
@Controller
@RequestMapping("/merPaid/merPaid")
public class MerPaidController extends BaseController
{
    private String prefix = "merPaid/merPaid";

    @Autowired
    private IMerPaidService merPaidService;  //出款表
    @Resource
    private IAccountsService accountsService;   //账户表
    @Resource
    private IQbBankInfoService bankInfoService; //支行总行表

    @RequiresPermissions("merPaid:merPaid:view")
    @GetMapping()
    public String merPaid()
    {
        return prefix + "/merPaid";
    }

    /**
     * 查询打款数据列表
     */
    @RequiresPermissions("merPaid:merPaid:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(MerPaid merPaid)throws Exception
    {
        startPage();
        if (StringUtils.isNotEmpty(merPaid.getScreenNum())){
            merPaid.setScreenNum(EncryptUtils.getEncrypt(merPaid.getScreenNum()));
        }
        List<MerPaid> list = merPaidService.selectMerPaidList(merPaid);
        for(int i=0;i<list.size();i++){
            list.get(i).setScreenNum(Utils.dealCard(EncryptUtils.getDecrypt(list.get(i).getScreenNum())));
        }
        return getDataTable(list);
    }

    /**
     * 导出打款数据列表
     */
    @RequiresPermissions("merPaid:merPaid:export")
    @Log(title = "打款数据", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(MerPaid merPaid)
    {
        List<MerPaid> list = merPaidService.selectMerPaidList(merPaid);
        ExcelUtil<MerPaid> util = new ExcelUtil<MerPaid>(MerPaid.class);
        return util.exportExcel(list, "merPaid");
    }

    /**
     * 新增打款数据
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存打款数据
     */
    @RequiresPermissions("merPaid:merPaid:add")
    @Log(title = "打款数据", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(MerPaid merPaid)
    {
        return toAjax(merPaidService.insertMerPaid(merPaid));
    }

    /**
     * 修改打款数据
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        MerPaid merPaid = merPaidService.selectMerPaidById(id);
        mmap.put("merPaid", merPaid);
        return prefix + "/edit";
    }

    /**
     * 修改保存打款数据
     */
    @RequiresPermissions("merPaid:merPaid:edit")
    @Log(title = "打款数据", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(MerPaid merPaid)
    {
        return toAjax(merPaidService.updateMerPaid(merPaid));
    }

    /**
     * 删除打款数据
     */
    @RequiresPermissions("merPaid:merPaid:remove")
    @Log(title = "打款数据", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(merPaidService.deleteMerPaidByIds(ids));
    }


    /**
     * 修改结算信息
     * @Param [merNo, mmap]
     * @Return java.lang.String
     */
    @GetMapping("/updateBank/{settleSerial}")
    @Log(title = "前往修改结算页面", businessType = BusinessType.UPDATE)
    public String updateBank(@PathVariable("settleSerial") String settleSerial, ModelMap mmap) throws Exception {
        //根据流水获取当前流水所对应的结算卡
        MerPaid merPaid1 = new MerPaid();
        merPaid1.setSettleSerial(settleSerial);
        List<MerPaid> merPaids = merPaidService.selectMerPaidList(merPaid1);
        if (CollectionUtils.isEmpty(merPaids)) {
            throw  new BusinessException("当前出款流水不存在");
        }

        MerPaid merPaid = merPaids.get(0);
        //总行信息
        QbBankInfo qbBankInfo = new QbBankInfo();
        qbBankInfo.setSubbranchName(merPaid.getBankName());
        List<QbBankInfo> qbBankInfos = bankInfoService.selectQbBankInfoList(qbBankInfo);
        if (CollectionUtils.isEmpty(qbBankInfos)) {
            throw  new BusinessException("银行卡信息错误");
        }
        String bankName = qbBankInfos.get(0).getBankName();
        mmap.put("settleSerial",settleSerial);          //出款流水
        mmap.put("bankHeadname",bankName);              //总行
        mmap.put("bankName",merPaid.getBankName());     //所属支行
        mmap.put("bankNum", merPaid.getBankNum());      //支行行号
        mmap.put("screenNum",EncryptUtils.getDecrypt(merPaid.getScreenNum()));   //结算卡号
        mmap.put("screenName",merPaid.getScreenName()); //结算人名
        mmap.put("id",merPaid.getId());                 //主键
        return prefix + "/updateBank";
    }


    /**
     * 修改出款记录结算信息
     */
    @RequiresPermissions("merPaid:merPaid:remove")
    @Log(title = "修改出款记录结算信息", businessType = BusinessType.UPDATE)
    @PostMapping( "/updateMerPaid")
    @ResponseBody
    public AjaxResult updateMerPaid(@RequestParam Map param) throws Exception {
        MerPaid merPaid = new MerPaid();
        merPaid.setBankName(String.valueOf(param.get("bankName")));
        merPaid.setBankNum(String.valueOf(param.get("bankNum")));
        merPaid.setId(Long.parseLong(String.valueOf(param.get("id"))));
        merPaid.setScreenName(String.valueOf(param.get("screenName")));
        merPaid.setScreenNum(EncryptUtils.getEncrypt(String.valueOf(param.get("screenNum"))).trim());
        return toAjax(merPaidService.updateMerPaid(merPaid));
    }


    /**
     * 发起未知查询
     * @Param [param]
     * @Return com.hjm.framework.web.domain.AjaxResult
     */
    @RequiresPermissions("merPaid:merPaid:queryUnknown")
    @Log(title = "发起未知查询", businessType = BusinessType.UPDATE)
    @PostMapping( "/queryUnknown")
    @ResponseBody
    public AjaxResult queryUnknown() throws Exception {
        //获取所有未知数据
        MerPaid merPaid = new MerPaid();
        merPaid.setSettleState("0");
        merPaidService.queryUnknown(merPaid);
        return success("查询处理中,请稍后进行查询..............");
    }



    /**
     * 失败重新出款
     * @Param [param]
     * @Return com.hjm.framework.web.domain.AjaxResult
     */
    @RequiresPermissions("merPaid:merPaid:errorAgain")
    @Log(title = "失败重新出款", businessType = BusinessType.UPDATE)
    @PostMapping( "/errorAgain")
    @ResponseBody
    public AjaxResult errorAgain() throws Exception {
        //获取所有未知数据
        MerPaid merPaid = new MerPaid();
        merPaid.setSettleState("-2");
        merPaidService.errorAgain(merPaid);
        return AjaxResult.success("出款处理中.....");
    }







}
